from tortoise import BaseDBAsyncClient


async def upgrade(db: BaseDBAsyncClient) -> str:
    return """
        CREATE TABLE IF NOT EXISTS `admin_admin_log` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `method` VARCHAR(10) NOT NULL,
    `uid` INT NOT NULL,
    `url` VARCHAR(255) NOT NULL,
    `desc` LONGTEXT,
    `ip` VARCHAR(255) NOT NULL,
    `success` INT NOT NULL,
    `user_agent` LONGTEXT,
    `create_time` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `admin_dept` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '部门ID',
    `parent_id` INT NOT NULL COMMENT '父级编号' DEFAULT 0,
    `dept_name` VARCHAR(50) NOT NULL COMMENT '部门名称',
    `sort` INT NOT NULL COMMENT '排序' DEFAULT 0,
    `leader` VARCHAR(50) NOT NULL COMMENT '负责人',
    `phone` VARCHAR(20) NOT NULL COMMENT '联系方式',
    `email` VARCHAR(50) NOT NULL COMMENT '邮箱',
    `status` INT NOT NULL COMMENT '状态(1开启,0关闭)' DEFAULT 1,
    `remark` LONGTEXT COMMENT '备注',
    `address` VARCHAR(255) COMMENT '详细地址',
    `create_at` DATETIME(6) NOT NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6),
    `update_at` DATETIME(6) NOT NULL COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `admin_dict_data` (
    `id` BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `data_label` VARCHAR(255) NOT NULL COMMENT '字典类型名称',
    `data_value` VARCHAR(255) NOT NULL COMMENT '字典类型标识',
    `type_code` VARCHAR(255) NOT NULL COMMENT '字典类型描述',
    `is_default` INT NOT NULL COMMENT '是否默认' DEFAULT 0,
    `enable` INT NOT NULL COMMENT '是否开启' DEFAULT 1,
    `remark` VARCHAR(255) COMMENT '备注',
    `create_time` DATETIME(6) NOT NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6),
    `update_time` DATETIME(6) NOT NULL COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `admin_dict_type` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `type_name` VARCHAR(255) NOT NULL COMMENT '字典类型名称',
    `type_code` VARCHAR(255) NOT NULL COMMENT '字典类型标识',
    `description` VARCHAR(255) NOT NULL COMMENT '字典类型描述',
    `enable` INT NOT NULL COMMENT '是否开启' DEFAULT 1,
    `create_time` DATETIME(6) NOT NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6),
    `update_time` DATETIME(6) NOT NULL COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `admin_operation_log` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `method` VARCHAR(10) NOT NULL COMMENT '请求方式',
    `user_id` INT COMMENT '用户编号',
    `url` VARCHAR(255) NOT NULL COMMENT '请求地址',
    `request_id` VARCHAR(64) NOT NULL COMMENT '请求ID',
    `summary` VARCHAR(255) COMMENT '路由描述',
    `params` LONGTEXT COMMENT '请求参数',
    `cost_time` DOUBLE COMMENT '耗时',
    `browser` VARCHAR(255) COMMENT '浏览器',
    `system` VARCHAR(255) COMMENT '系统',
    `tags` VARCHAR(255) COMMENT '标签',
    `route_name` VARCHAR(255) COMMENT '路由名称',
    `status` INT NOT NULL COMMENT '状态' DEFAULT 1,
    `content_length` VARCHAR(32) COMMENT '内容长度',
    `country` VARCHAR(64) COMMENT '国家',
    `region` VARCHAR(64) COMMENT '大区',
    `province` VARCHAR(64) COMMENT '省份',
    `city` VARCHAR(64) COMMENT '城市',
    `isp` VARCHAR(128) COMMENT '运营商',
    `create_time` DATETIME(6) NOT NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6)
) CHARACTER SET utf8mb4 COMMENT='操作记录';
CREATE TABLE IF NOT EXISTS `admin_photo` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `name` VARCHAR(255) NOT NULL,
    `href` VARCHAR(255),
    `mime` VARCHAR(50) NOT NULL,
    `size` VARCHAR(30) NOT NULL,
    `create_time` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `admin_power` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `name` VARCHAR(255) NOT NULL COMMENT '权限名称',
    `type` VARCHAR(1) NOT NULL COMMENT '权限类型',
    `code` VARCHAR(30) NOT NULL COMMENT '权限标识',
    `url` VARCHAR(255) NOT NULL COMMENT '权限路径',
    `open_type` VARCHAR(10) NOT NULL COMMENT '打开方式',
    `parent_id` INT NOT NULL COMMENT '父类编号' DEFAULT 0,
    `icon` VARCHAR(128) COMMENT '图标',
    `sort` INT NOT NULL COMMENT '排序' DEFAULT 0,
    `create_time` DATETIME(6) NOT NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6),
    `update_time` DATETIME(6) NOT NULL COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    `enable` INT NOT NULL COMMENT '是否开启' DEFAULT 1
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `admin_role` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `name` VARCHAR(255) NOT NULL COMMENT '角色名称',
    `code` VARCHAR(255) NOT NULL COMMENT '角色标识',
    `enable` INT NOT NULL COMMENT '是否启用' DEFAULT 1,
    `remark` VARCHAR(255) COMMENT '备注',
    `details` VARCHAR(255) COMMENT '详情',
    `sort` INT NOT NULL COMMENT '排序' DEFAULT 0,
    `create_time` DATETIME(6) NOT NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6),
    `update_time` DATETIME(6) NOT NULL COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `admin_user` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `username` VARCHAR(20) NOT NULL COMMENT '用户名',
    `realname` VARCHAR(20) NOT NULL COMMENT '真实名字',
    `avatar` VARCHAR(255) NOT NULL COMMENT '头像' DEFAULT '/static/system/admin/images/avatar.jpg',
    `remark` VARCHAR(255) COMMENT '备注',
    `password_hash` VARCHAR(128) NOT NULL COMMENT '哈希密码',
    `enable` INT NOT NULL COMMENT '启用' DEFAULT 0,
    `create_at` DATETIME(6) NOT NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6),
    `update_at` DATETIME(6) NOT NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    `dept_id` INT,
    CONSTRAINT `fk_admin_us_admin_de_d2b6bd0e` FOREIGN KEY (`dept_id`) REFERENCES `admin_dept` (`id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `admin_mail` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `receiver` VARCHAR(1024) NOT NULL COMMENT '收件人邮箱',
    `subject` VARCHAR(128) NOT NULL COMMENT '邮件主题',
    `content` LONGTEXT NOT NULL COMMENT '邮件正文',
    `is_success` BOOL NOT NULL COMMENT '是否成功' DEFAULT 1,
    `exception` LONGTEXT NOT NULL COMMENT '异常信息',
    `create_at` DATETIME(6) NOT NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6),
    `user_id` INT,
    CONSTRAINT `fk_admin_ma_admin_us_be1edae5` FOREIGN KEY (`user_id`) REFERENCES `admin_user` (`id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `aerich` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `version` VARCHAR(255) NOT NULL,
    `app` VARCHAR(100) NOT NULL,
    `content` JSON NOT NULL
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `admin_role_power` (
    `admin_role_id` INT NOT NULL,
    `power_id` INT NOT NULL,
    FOREIGN KEY (`admin_role_id`) REFERENCES `admin_role` (`id`) ON DELETE CASCADE,
    FOREIGN KEY (`power_id`) REFERENCES `admin_power` (`id`) ON DELETE CASCADE,
    UNIQUE KEY `uidx_admin_role__admin_r_9f5f0f` (`admin_role_id`, `power_id`)
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `admin_user_role` (
    `user_id` INT NOT NULL,
    `role_id` INT NOT NULL,
    FOREIGN KEY (`user_id`) REFERENCES `admin_user` (`id`) ON DELETE CASCADE,
    FOREIGN KEY (`role_id`) REFERENCES `admin_role` (`id`) ON DELETE CASCADE,
    UNIQUE KEY `uidx_admin_user__user_id_f40ed0` (`user_id`, `role_id`)
) CHARACTER SET utf8mb4;"""


async def downgrade(db: BaseDBAsyncClient) -> str:
    return """
        """
